<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-08-02 20:04
 *
 * Version: 2.0
 */

namespace App\Lib;


class CreateStaticNumber
{
	private static $sourceString;
    private static $yCode;
    public function __construct()
    {
    	self::$yCode = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];

    	self::$sourceString = 'FRSTUV6MW7X89KLYZ5ECDG3HQA4B1OPIJ2N';
    }

	/**
	 * 生成卡号
	 * @return string
	 */
    private static function makeCardNo(){
    	return self::$yCode[intval(date('Y')) - 2019] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
    }

	/**
	 * 生成订单号
	 * @return string
	 */
	private static function makeOrderNo(){
		// 订单号码 YY-MM-DD-NNNN-CC
		$order_id_main = date('ymd') . rand(1000, 9999);
		//订单号码主体长度
		$order_id_len = strlen($order_id_main);
		$order_id_sum = 0;
		for ($i = 0; $i < $order_id_len; $i++) {
			$order_id_sum += (int)(substr($order_id_main, $i, 1));
		}
		//唯一订单号码（YYMMDDNNNNCC）
		return $order_id_main . str_pad((100 - $order_id_sum % 100) % 100, 2, '0', STR_PAD_LEFT);
	}



	/**
	 * 生成服务费充值订单号
	 * @return string
	 */
	private static function makeServiceChargeOrderNo (){
		return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
	}




	private static function makeUserNo($userId)
	{
		$num = $userId;
		$code = '';
		while ($num) {
			$mod = $num % 36;
			$num = (int)($num / 36);
			$code = self::$sourceString[$mod].$code;
		}
		//判断code的长度
		if (empty($code[4]))
			str_pad($code, 5, '0', STR_PAD_LEFT);
		return $code;
	}


	private static function makeCouponCode($len=12){
		$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
		$string=time();
		for(;$len>=1;$len--)
		{
			$position=rand()%strlen($chars);
			$position2=rand()%strlen($string);
			$string=substr_replace($string,substr($chars,$position,1),$position2,0);
		}
		return $string;
	}

	/**
	 * 生成编号
	 * @param $param
	 * @param int $userId
	 * @return string
	 */
	public static function makeNo($param,$userId=0){
		$no = '';
		switch ($param){
			case 'order':
				$no = self::makeOrderNo();
				break;
			case 'card':
				$no =  self::makeCardNo();
				break;
			case 'user':
				$no = self::makeUserNo($userId);
				break;
			case 'fee':
				$no = self::makeServiceChargeOrderNo();
				break;
			case 'coupon':
				$no = self::makeCouponCode();
				break;
		}
		return $no;
	}

}